/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;
import entidades.*;
import Controladores.*;
import Controladores.exceptions.NonexistentEntityException;
import Controladores.exceptions.PreexistingEntityException;
import java.awt.Dimension;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
 *
 * @author Gatoper
 */
public class JPUOwner extends javax.swing.JPanel {

    /**
     * Creates new form JPUOwner
     */
    public JPUOwner() {
        initComponents();
        this.setSize(new Dimension(300, 400));
    }
    
    public JPUOwner(Owner own) {
        this.own=own;
        initComponents();
        String[] bday=own.getBirthday().split(" ");
        String[] fechas=bday[0].split("-");
        CBDay.setSelectedIndex(Integer.valueOf(fechas[2])-1);
        CBmonth.setSelectedIndex(Integer.valueOf(fechas[1])-1);
        CByear.setSelectedIndex(Integer.valueOf(fechas[0])-1991);
        TSSN.setText(own.getSsn());
        Tbirthplace.setText(own.getBirthplace());
        Tlastname.setText(own.getLasname());
        Tname.setText(own.getName());
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("HPVAS");
        OwnerJpaController OwnJpaC=new OwnerJpaController(emf);
        Tpets.setModel(OwnJpaC.findPetsAndPrices(own));
        //LPets.setText(((Pet)(own.getPet().get(0))).getName());
        this.setSize(new Dimension(300, 400));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Lname = new javax.swing.JLabel();
        Llastname = new javax.swing.JLabel();
        LSSN = new javax.swing.JLabel();
        Lbirthday = new javax.swing.JLabel();
        Lbirthplace = new javax.swing.JLabel();
        CBDay = new javax.swing.JComboBox();
        CBmonth = new javax.swing.JComboBox();
        CByear = new javax.swing.JComboBox();
        Tname = new javax.swing.JTextField();
        Tlastname = new javax.swing.JTextField();
        TSSN = new javax.swing.JTextField();
        Tbirthplace = new javax.swing.JTextField();
        Bupdate = new javax.swing.JButton();
        BDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        Tpets = new javax.swing.JTable();

        Lname.setText("Name :");

        Llastname.setText("Lastname :");

        LSSN.setText("SSN");

        Lbirthday.setText("Birthday :");

        Lbirthplace.setText("Birthplace :");

        CBDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12" }));

        CBmonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4","5", "6", "7", "8","9", "10", "11", "12" }));

        CByear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1991", "1992", "1993", "1994","1995", "1996", "1997", "1998" }));

        Sletras(Tname);
        Tname.setEnabled(false);
        Tname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TnameActionPerformed(evt);
            }
        });
        Tname.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent evt) {
                TIdFocusGained(evt);
            }
        });

        Sletras(Tlastname);

        Snumero(TSSN);
        TSSN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TSSNActionPerformed(evt);
            }
        });

        Sletras(Tbirthplace);

        Bupdate.setText("Update");
        Bupdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BupdateActionPerformed(evt);
            }
        });

        BDelete.setText("Delete");
        BDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDeleteActionPerformed(evt);
            }
        });

        Tpets.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(Tpets);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Lbirthplace)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Tbirthplace))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Lbirthday)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(CBDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LSSN)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(TSSN))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Llastname)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Tlastname))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Lname)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Tname, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Bupdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BDelete)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lname)
                    .addComponent(Tname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Llastname)
                    .addComponent(Tlastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LSSN)
                    .addComponent(TSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lbirthday)
                    .addComponent(CBDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBmonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CByear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lbirthplace)
                    .addComponent(Tbirthplace, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Bupdate)
                    .addComponent(BDelete))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void TnameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TnameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TnameActionPerformed

    private void TSSNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TSSNActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TSSNActionPerformed

    private void BupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BupdateActionPerformed
        // TODO add your handling code here:
        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties = new HashMap<>();
        emf = Persistence.createEntityManagerFactory("HPVAS");
        em = emf.createEntityManager();
         Owner Own=new Owner(this.Tname.getText(),this.Tlastname.getText(),this.TSSN.getText(),this.CByear.getSelectedItem().toString()+"-"+this.CBmonth.getSelectedItem().toString()+"-"+this.CBDay.getSelectedItem().toString()+" 00:00:00",this.Tbirthplace.getText());
        OwnerJpaController OwnJpaC=new OwnerJpaController(emf);
        try {        
            OwnJpaC.edit(Own);
            JOptionPane.showMessageDialog(this, "Perfecto.");
        } catch (PreexistingEntityException ex) {
            JOptionPane.showMessageDialog(this, "Esa Entidad no existia");
            Logger.getLogger(JPIOwner.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error durante la ejecucion");
            Logger.getLogger(JPIOwner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BupdateActionPerformed

    private void BDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDeleteActionPerformed
        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties = new HashMap<>();
        emf = Persistence.createEntityManagerFactory("HPVAS");
        em = emf.createEntityManager();         
        OwnerJpaController OwnJpaC=new OwnerJpaController(emf);
        try {
            OwnJpaC.destroy(own.getName());
            JOptionPane.showMessageDialog(this, "Perfecto.");
        } catch (NonexistentEntityException ex) {
            JOptionPane.showMessageDialog(this, "Esa Entidad no existia");
            Logger.getLogger(JPUOwner.class.getName()).log(Level.SEVERE, null, ex);
        }catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error durante la ejecucion");
            Logger.getLogger(JPIOwner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BDeleteActionPerformed

    private void TIdFocusGained(FocusEvent evt) {
        this.Tlastname.setEnabled(false);
        this.Tbirthplace.setEnabled(false);
        this.TSSN.setEnabled(false);
        this.CBDay.setEnabled(false);
        this.CBmonth.setEnabled(false);
        this.CByear.setEnabled(false);
        this.Bupdate.setEnabled(false);
    }    public void Snumero(JTextField a){
    a.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e){
        char c=e.getKeyChar();
        if (Character.isLetter(c)) {
            e.consume();
        }
    }
    });
    }
    public void Sletras(JTextField a){
    a.addKeyListener(new KeyAdapter() {
    public void keyTyped(KeyEvent e){
        char c=e.getKeyChar();
        if (Character.isDigit(c)) {
            e.consume();
        }
    }
    });
    } 
    private Owner own;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BDelete;
    private javax.swing.JButton Bupdate;
    private javax.swing.JComboBox CBDay;
    private javax.swing.JComboBox CBmonth;
    private javax.swing.JComboBox CByear;
    private javax.swing.JLabel LSSN;
    private javax.swing.JLabel Lbirthday;
    private javax.swing.JLabel Lbirthplace;
    private javax.swing.JLabel Llastname;
    private javax.swing.JLabel Lname;
    private javax.swing.JTextField TSSN;
    private javax.swing.JTextField Tbirthplace;
    private javax.swing.JTextField Tlastname;
    private javax.swing.JTextField Tname;
    private javax.swing.JTable Tpets;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
